package com.lpj.article.dao;

import com.lpj.article.entity.Comment;
import com.lpj.article.service.CommentService;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class test {

    @Autowired
    private CommentService commentService;

    @Test
    public void findAll() {
        System.out.println(commentService.findAll());
    }

    @Test
    public void saveComment() {
        Comment comment = new Comment();
        comment.setContent("冲冲冲");
        comment.setParentid("1");
        commentService.saveComment(comment);
    }

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void updateComment() {
        /*Comment comment = new Comment();
        comment.setId("5ea2a89436cc9f7b6b3518d6");
        comment.setNickname("你说落叶的一生只是为了归根吗~");
        commentService.updateComment(comment);*/

        //查询对象
        Query query = Query.query(Criteria.where("_id").is("5ea2552ffbf133513cd1a5dd"));
        Update update = new Update();
        //局部修改
        update.set("content", "没有撤退可言");

        mongoTemplate.updateFirst(query, update, "comment");
    }

    @Test
    public void deleteCommentById() {
        commentService.deleteCommentById("5ea2a89436cc9f7b6b3518d6");
    }

    @Test
    public void findCommentById() {
        System.out.println(commentService.findCommentById("1"));
    }

    @Test
    public void findCommentListPageByParentid() {

        Page<Comment> pageResponse = commentService.findCommentListPageByParentid("1", 1, 2);
        System.out.println("----总记录数：" + pageResponse.getTotalElements());
        System.out.println("----当前页数据：" + pageResponse.getContent());

    }

    @Test
    public void updateCommentLikenumThumbup() {
        //给id为“1”的评论点赞，赞+1
        commentService.updateCommentLikenumThumbup("1");
    }
}